package com.smarthealthring.mm.api;

import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;


/**
 * Created by sunxl8 on 2017/3/20.
 *
 * 解析有时平台返回的content该为空时平台却返回了""造成解析错误
 */

public class ResultJsonDeser implements JsonDeserializer {

    @Override
    public ApiResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        if (json.isJsonObject()) {
            JsonObject object = json.getAsJsonObject();
            if (object.get("content")!=null && object.get("content").isJsonPrimitive()) {
                ApiResponse response = new ApiResponse();
                response.setCode(object.get("code") != null ? object.get("code").getAsString() : "");
                response.setToken(object.get("token").getAsString() != null ? object.get("token").getAsString() : "");
                response.setMsg(object.get("msg").getAsString() != null ? object.get("msg").getAsString() : "");
                response.setEmpId(object.get("empId").getAsString() != null ? object.get("empId").getAsString() : "");
                response.setIsSuccess(object.get("isSuccess").getAsBoolean());
                response.setContent(null);
                return response;
            }
        }
        return new Gson().fromJson(json, typeOfT);

    }
}
